package com.rankeiot.developer.modules;

import com.rankeiot.core.data.Item;
import com.rankeiot.core.translator.FormatTranslator;
import com.rankeiot.core.translator.MultipleTranslator;
import com.rankeiot.core.translator.SimpleTranslator;
import com.rankeiot.core.translator.Translator;
import com.rankeiot.core.util.CollectionUtil;
import com.rankeiot.core.util.StringUtil;
import com.rankeiot.platform.service.CacheDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.annotation.MagicModule;
import org.ssssssss.script.annotation.Comment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Component
@MagicModule("translate")
@RequiredArgsConstructor
public class TranslatorModule {
    final CacheDataService cacheDataService;
    @Comment("获取数据翻译器")
    public Translators create(){
        return new Translators();
    }
    class Translators{

        Map<String,Translator> translatorMap=new HashMap<>();
        @Comment("添加字段翻译器")
        public Translators add(String field,Translator translator){
            translatorMap.put(field,translator);
            return this;
        }
        @Comment("添加Map字段翻译器")
        public Translators addMap(String field,Map map,@Comment(name = "encode", value = "是否编码") boolean encode){
            add(field,formMap(map,encode));
            return this;
        }
        @Comment("添加字典字段翻译器")
        public Translators addDict(String field,String dict,boolean encode){
            add(field,dict(dict,encode));
            return this;
        }
        @Comment("添加机构字段翻译器")
        public Translators addBranch(String field,String parent,boolean encode){
            add(field,branch(parent,encode));
            return this;
        }
        @Comment("添加格式字段翻译器")
        public Translators addFormat(String field,String format){
            add(field,format(format));
            return this;
        }
        @Comment("翻译数据")
        public List<Map> translate(List<Map> source){
            for(Map m:source){
                for(Map.Entry<String,Translator> e:translatorMap.entrySet()){
                    Object sourceValue= m.get(e.getKey());
                    if(sourceValue!=null){
                        Object targetValue = e.getValue().translate(m,e.getKey(),sourceValue);
                        if(targetValue!=null){
                            m.put(e.getKey(),targetValue);
                        }
                    }
                }
            }
            return source;
        }
    }
    public Translator formMap(Map map,boolean encode){
        return new SimpleTranslator(map,encode);
    }
    public Translator format(String format){
        return new FormatTranslator(format);
    }
    public Translator dict(String dict,boolean encode){
        List<Item> dists= cacheDataService.allCodes().get(dict);
        if(dists==null){
            return null;
        }
        return new SimpleTranslator(CollectionUtil.toMap(dists,Item::getKey,Item::getTitle),encode);
    }
    public Translator branch(String parent,boolean decode){
        List<Item> dists= cacheDataService.getBaranchs(parent);
        if(dists==null){
            return null;
        }
        return new SimpleTranslator(CollectionUtil.toMap(dists,Item::getKey,Item::getTitle),decode);
    }

    public Translator multiple(Translator source){
        //return new MultipleTranslator()
        return (target, filedName, value) -> {
            if(value==null){
                return null;
            }
            String[] vs=value.toString().split(",");
            List<Object> dist=new ArrayList<>();
            for(String k:vs){
                dist.add(source.translate(null,null,k));
            }
            return StringUtil.join(dist);
        };
    }
}
